#ifndef __STK36C61_H__
#define __STK36C61_H__

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>


#define  STK_PROXIMITY_THREASOLD    50
#define  STK_COLOR_THREASOLD        200




#define STK36C61_ADDR           0x48

/*ALSPS REGS*/
#define STK_STATE_REG 			0x00
#define STK_PSCTRL_REG 			0x01
#define STK_ALSCTRL_REG 		0x02
#define STK_LEDCTRL_REG 		0x03
#define STK_INT_REG 			0x04
#define STK_WAIT_REG 			0x05
#define STK_THDH1_PS_REG 		0x06
#define STK_THDH2_PS_REG 		0x07
#define STK_THDL1_PS_REG 		0x08
#define STK_THDL2_PS_REG 		0x09
#define STK_THDH1_ALS_REG 		0x0A
#define STK_THDH2_ALS_REG 		0x0B
#define STK_THDL1_ALS_REG 		0x0C
#define STK_THDL2_ALS_REG 		0x0D
#define STK_FLAG_REG 			0x10
#define STK_DATA1_PS_REG	 	0x11
#define STK_DATA2_PS_REG 		0x12
#define STK_DATA1_ALS_REG 		0x13
#define STK_DATA2_ALS_REG 		0x14
#define STK_DATA1_OFFSET_REG 	0x1D
#define STK_DATA2_OFFSET_REG 	0x1E
#define STK_PDT_ID_REG 			0x3E
#define STK_SW_RESET_REG		0x80

/* Define state reg */
#define STK_STATE_EN_WAIT_SHIFT  	2
#define STK_STATE_EN_ALS_SHIFT  	1
#define STK_STATE_EN_PS_SHIFT  	    0

#define STK_STATE_EN_WAIT_MASK	0x04
#define STK_STATE_EN_ALS_MASK	0x02
#define STK_STATE_EN_PS_MASK	0x01

#define STK_INT_PS_MODE1			0x01
#define STK_INT_PS_MODE3			0x03
#define STK_INT_PS_MODE4			0x04
#define STK_INT_ALS				    0x08

#define STK_FLG_ALSDR_MASK		0x80
#define STK_FLG_PSDR_MASK		0x40
#define STK_FLG_ALSINT_MASK		0x20
#define STK_FLG_PSINT_MASK		0x10
#define STK_FLG_NF_MASK			0x01



typedef struct{
	  uint8_t id;
		bool is_init;
	  bool status;
	  bool last_status;
		bool is_black_desktop;
	
		uint16_t proximity_data;
		uint16_t last_proximity_data;
		uint16_t ambient_light;
		uint16_t last_ambient_light_data;
		uint16_t ir_data;
	
		uint16_t red_light;
		uint16_t green_light;
		uint16_t blue_light;
		uint16_t c_light;
	
		uint16_t pins;
	  void (*Set_Out)(uint16_t pins,bool level);  // 设置引脚状态

 // 独立的状态和时间信息

    float filtered_proximity;
    uint32_t current_state;           // 当前状态
	uint32_t last_state;              // 上一次的状态
    uint32_t last_state_change_time;  // 上次状态改变的时间
    uint32_t last_black_surface_time; // 上次黑色桌面检测时间
	
	
	
}_stk36c61_t;



typedef struct{

	 uint8_t i2c_id;
	_stk36c61_t stk36c61[4];

}stk36c61_t;



void STK36C61_Init();

void STK36C61_Task();

//uint32_t get_current_time_ms();
//float filter_proximity_data(_ap3216c_t * ap3216,uint16_t current_value);

 void STK_Init(void);
void STK_sw_reset(void);
void STK_PowerOn(void);
void STK_Show_Allreg(void);

/*ps enable interface*/
uint32_t STK_enable_PS(void);
uint32_t STK_DisEnable_PS(void);

/*als enable interface*/
uint32_t STK_enable_ALS(void);
uint32_t STK_DisEnable_ALS(void);
uint16_t STK_Read_PS(void);
uint16_t STK_Read_ALS(void);

uint16_t get_ps_data(void);
uint16_t read_als_data(void);


    /* Ambient light methods */
    bool stk_readAmbientLight(uint16_t *val);
    bool stk_readRedLight(uint16_t *val);
    bool stk_readGreenLight(uint16_t *val);
    bool stk_readBlueLight(uint16_t *val);
	bool stk_readCLight(uint16_t *val);
    
    /* Proximity methods */
    bool stk_readProximity(uint16_t *val);


/* Raw I2C Commands */
static bool wireWriteByte(uint8_t val);
static bool wireWriteDataByte(uint8_t reg, uint8_t val);
static bool wireWriteDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
static bool wireReadDataByte(uint8_t reg, uint8_t *val);
static int wireReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len);

 uint8_t  STK_ReadReg(uint8_t adr);
 uint8_t STK_WriteReg(uint8_t adr, uint8_t value);

#endif